<?php

namespace App\Console\Commands;

use App\Models\Archive;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class ChangeArchiveCategory extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zx:change-archive-category {--key=*} {--category=1}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动修改图片分类';

    public $changeTotal = 0;

    public $default = ['头像', '男头', '女头', '漫头', '情头', '闺蜜头'];
    public $defaultChangeTo = 1;
    public $keys = [];
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->keys = count($this->option('key')) > 0 ? $this->option('key') : $this->default;
        $this->defaultChangeTo = $this->option('category');
        Archive::with('category')->cursor()->each(function ($archive) {
            if ($archive->category->title !== '头像' && Str::contains($archive->title, $this->keys)) {
                $archive->category_id = $this->defaultChangeTo;
                $archive->save();
                $this->info(sprintf('文档 %s 移动到头像分类', $archive->title));
                $this->changeTotal++;
            }
        });
        $this->info(sprintf('操作完成共处理了 %d 个文档', $this->changeTotal));
    }
}
